package Demo_0422;

import Demo_0415.SqStackClass;

/**
 * @author:zhengyilong
 * @email:1797344574@qq.com
 * @phone:13479394730
 * @desc:
 * @DateTime:2022/4/22 9:48
 **/
public class SqQueueClass<E> {
    final int maxSize = 100;
    private E [] data;
    private int front,rear;

    public SqQueueClass(){
        data = (E[]) new Object[maxSize];
        front = -1;
        rear = -1;
    }

    public boolean empty(){
        return front == rear;
    }

    public void push(E e){
        //判断是否装满
        if (rear == maxSize){
            throw new IllegalArgumentException("队列满了");
        }
        rear++;
        data[rear] = e;
    }

    public E pop(){
        //判断是否为空队列
        if (empty()){
            throw new IllegalArgumentException("队列为空");
        }
        front++;
        return (E)data[front];
    }

    public E peek(){
        if (empty()){
            throw new IllegalArgumentException("队列为空");
        }
        return (E)data[front+1];
    }

    @Override
    public String toString() {
        String answer = "";
        for(int i = front+1;i<=rear;i++){
            answer += data[i] + " ";
        }
        return answer;
    }
}

